package com.zy.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.test.entity.Student;
import com.zy.test.entity.Teacher;
import com.zy.test.service.TeacherService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 教师表
 * 教师ID，教师号，姓名，年龄，性别，所教科目，联系方式，备注。教师id为主键(Teacher)表控制层
 *
 * @author jhr
 * @since 2023-04-03 16:32:30
 */
@RestController
@RequestMapping("teacher")
public class TeacherController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TeacherService teacherService;

    /**
     * 分页查询所有数据
     *
     * @param teacher 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectByTeacherPaging")
    public R selectAllByPaging(Teacher teacher) {
        Page<Teacher> page = new Page<>(teacher.getPageNum(), teacher.getPageSize());
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(!ObjectUtils.isEmpty(teacher.getTeCode())){
            teacherLambdaQueryWrapper.eq(Teacher::getTeCode,teacher.getTeCode());
        }
        if(!ObjectUtils.isEmpty(teacher.getTeName())){
            teacherLambdaQueryWrapper.like(Teacher::getTeName,teacher.getTeName());
        }
        if(!ObjectUtils.isEmpty(teacher.getNumberid())){
            teacherLambdaQueryWrapper.like(Teacher::getNumberid,teacher.getNumberid());
        }
        return success(this.teacherService.page(page, teacherLambdaQueryWrapper));
    }

    /**
     * 查询所有教师数据
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public R selectAll() {
        return success(this.teacherService.list());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.teacherService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param teacher 实体对象
     * @return 新增结果
     */
    @PostMapping("/insertTeacher")
    public R insert(@RequestBody Teacher teacher) {
        return success(this.teacherService.save(teacher));
    }

    /**
     * 修改数据
     *
     * @param teacher 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    public R update(@RequestBody Teacher teacher) {
        return success(this.teacherService.updateById(teacher));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteByIds")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.teacherService.removeByIds(idList));
    }
}

